<?php

    /**
     *      [Discuz!] (C)2001-2099 Comsenz Inc.
     *      This is NOT a freeware, use is subject to license terms
     *      $Id: table_mobile_setting.php 31281 2012-08-03 02:29:27Z zhangjie $
     */

    if (!defined('IN_DISCUZ')) {
        exit('Access Denied');
    }

    class table_cloud_product extends discuz_table {

        public function __construct() {
            $this->_table = 'cloud_product';
            $this->_pk = 'cpid';

            parent::__construct();
        }

        //插入数据库
        public function insert($arr) {
            return DB::insert($this->_table, $arr, true);
        }

        //更新数据库
        public function update($id, $data) {
            $id = intval($id);
            if (!$data) {
                return;
            }
            return DB::update($this->_table, $data, DB::field($this->_pk, $id));
        }

        //查找某条记录
        public function fetch_info($id) {
            $id = intval($id);
            return DB::fetch_first("SELECT * FROM %t WHERE ".$this->_pk."=%d", array($this->_table, $id));
        }

        //根据keys称查询
        public function fetch_product_by_name($name, $cadid, $start, $limit) {
            return DB::fetch_all('SELECT * FROM ' . $this->_table . ' WHERE pname like %' . $name . '% or explain like %' . $name . '% or material like %' . $name . '%  or term like %' . $name . '%  AND cadid=' . $cadid . DB::limit($start, $limit));
        }

        //查询所有limt数据
        public function fetch_all_limit($cadid, $start = 0, $limit = 0) {
            return DB::fetch_all('SELECT * FROM ' . DB::table($this->_table) . ' where cadid=' . $cadid . ' order by ' . DB::order('lasttime', 'desc') . DB::limit($start, $limit));
        }

        //查询所有
        public function fetch_all_full($cadid) {
            return DB::fetch_all('SELECT cpid,name FROM ' . DB::table($this->_table) . ' where cadid=' . $cadid . ' order by ' . DB::order('lasttime', 'desc') );
        }


        //查询所有limt数据
        public function fetch_recommend_area_limit($cadid, $start = 0, $limit = 0) {
            return DB::fetch_all('SELECT * FROM ' . DB::table($this->_table) . ' where cadid=' . $cadid . ' and recommend = 1 and disabled =1  order by ' . DB::order('lasttime', 'desc') . DB::limit($start, $limit));
        }

        //查询所有推荐的并且排序推荐。
        public function fetch_recommend_area_orderby_limit($cadid,$order='',$start = 0, $limit = 0){
            $sql = "select * from ". DB::table($this->_table)." where cadid = ".$cadid .' and recommend = 1 and disabled =1';
            if($order!=''){
                if($order =='rebate'){
                    $sql = $sql . " order by ".DB::order($order);
                }else if($order == 'meanPrice'){
                    $sql = $sql . " order by ".DB::order($order);
                }else{
                    $sql = $sql . " order by ".DB::order($order,'DESC');
                }
            }
            $sql = $sql . " ". DB::limit($start, $limit);
            return DB::fetch_all($sql);
        }

        //查询所有limt数据
        public function fetch_all_order_limit($cadid, $order='', $start = 0, $limit = 0) {
            $sql = "select * from ". DB::table($this->_table)." where cadid = ".$cadid.' and recommend = 1 and disabled =1 ' ;
            if($order!=''){
                if($order =='rebate'){
                    $sql = $sql . " order by ".DB::order($order);
                }else if($order == 'meanPrice'){
                    $sql = $sql . " order by ".DB::order($order);
                }else{
                    $sql = $sql . " order by ".DB::order($order,'DESC');
                }
            }
            $sql = $sql . " ". DB::limit($start, $limit);
            return DB::fetch_all($sql);
        }

        //查询所有limt数据
        public function fetch_all_category_product($categoryid, $start = 0, $limit = 0, $order = '') {
            $sql_category = 'SELECT cpid FROM  ' . DB::table('cloud_product_category') . ' where ccid = ' . $categoryid;
            $category_product = DB::fetch_all($sql_category);
            if(empty($category_product)){
                return array();
            }
            $forCategory = '';
            foreach ($category_product as $val) {
                $forCategory .= $val['cpid'] . ',';
            }
            $forCategory = substr($forCategory, 0, strlen($forCategory) - 1);
            $order_sql = '';
            if ($order != '') {
                if ($order == 'meanPrice') {
                    $order_sql = ' order by ' . DB::order($order);
                } else if ($order == 'rebate'){
                    $order_sql = ' and rebate < 10  order by ' . DB::order($order);
                }else {
                    $order_sql = ' order by ' . DB::order($order, 'desc');
                }
            }
            $sql = 'SELECT * FROM ' . DB::table($this->_table) . ' where ' . $this->_pk . '  in (' . $forCategory . ')  and disabled=1 ' . $order_sql . ' ' . DB::limit($start, $limit);
            return DB::fetch_all($sql);
        }
        //查询所有limt数据
        public function fetch_all_by_category_product($categoryid, $order = '') {
            $sql_category = 'SELECT cpid FROM  ' . DB::table('cloud_product_category') . ' where ccid = ' . $categoryid;
            $category_product = DB::fetch_all($sql_category);
            if(empty($category_product)){
                return array();
            }
            $forCategory = '';
            foreach ($category_product as $val) {
                $forCategory .= $val['cpid'] . ',';
            }
            $forCategory = substr($forCategory, 0, strlen($forCategory) - 1);
            $order_sql = '';
            if ($order != '') {
                if ($order == 'meanPrice') {
                    $order_sql = ' order by ' . DB::order($order);
                } else if ($order == 'rebate'){
                    $order_sql = ' and rebate < 10  order by ' . DB::order($order);
                }else {
                    $order_sql = ' order by ' . DB::order($order, 'desc');
                }
            }
            $sql = 'SELECT * FROM ' . DB::table($this->_table) . ' where ' . $this->_pk . '  in (' . $forCategory . ')  and disabled=1 ' . $order_sql . ' ' ;
            return DB::fetch_all($sql);
        }

        //查询所有limt数据
        public function fetch_all_category_product_and_cadid($categoryid, $start = 0, $limit = 0, $order = '') {
            if ($categoryid == '') {
                $sql_category = 'SELECT cpid FROM  ' . DB::table('cloud_product_category');
            } else {
                $sql_category = 'SELECT cpid FROM  ' . DB::table('cloud_product_category') . ' where ccid = ' . $categoryid;
            }
            $category_product = DB::fetch_all($sql_category);
            if (empty($category_product)) {
                return array();
            }
            $forCategory = '';
            foreach ($category_product as $val) {
                $forCategory .= $val['cpid'] . ',';
            }
            $forCategory = substr($forCategory, 0, strlen($forCategory) - 1);
            $order_sql = '';
            if ($order != '') {
                if ($order == 'meanPrice') {
                    $order_sql = ' order by ' . DB::order($order);
                } else {
                    $order_sql = ' order by ' . DB::order($order, 'desc');
                }
            }
            $sql = 'SELECT * FROM ' . DB::table($this->_table) . ' where ' . $this->_pk . '  in (' . $forCategory . ')  and disabled=1 and cadid in(6,7) ' . $order_sql . ' ' ;
            //. DB::limit($start, $limit);
            return DB::fetch_all($sql);
        }



        public function fetch_info_byccid($cpid) {
            $cpid = intval($cpid);
            return DB::fetch_first('SELECT ccid FROM pre_cloud_product_category WHERE cpid=' . $cpid);
        }

        // 删除
        public function delete_by_id($id) {
            return DB::query("DELETE FROM %t WHERE " . $this->_pk . "=%d", array($this->_table, $id));
        }
        //查询count
        public function fetch_all_count($cadid) {
            return DB::fetch_first('SELECT count(cpid) as count FROM ' . DB::table($this->_table) . ' where cadid='. $cadid);
        }

        //查询count
        public function fetch_all_order_count($cadid) {
            return DB::fetch_first('SELECT count(cpid) as count FROM ' . DB::table($this->_table) . ' where cadid='. $cadid.'  and disabled =1');
        }

        //查询count
        public function fetch_recommend_area_orderby_count($cadid) {
            return DB::fetch_first('SELECT count(cpid) as count FROM ' . DB::table($this->_table) . ' where cadid='. $cadid  .' and recommend = 1 and disabled =1');
        }


        public function fetch_all_category_product_count($categoryid){
             $sql_category = 'SELECT count(cpid) as countnum FROM  ' . DB::table('cloud_product_category') . ' where ccid = ' . $categoryid;
            return DB::fetch_first($sql_category);
        }
    }